﻿
var Manage_Module = {
    openDialog: function (url, title, id, parentid) {
        if (id != null && id != undefined && id != "") {

        }
        url = url + "?id=" + id + "&parentid=" + parentid;
        $.pdialog.open(url, "module_menuadd", title, { mask: true, width: 820, height: 440 });
    },


    //快速保存（导航）
    fastSave: function (id) {
        if (id == null || id == undefined || id == "") {
            alertMsg.info('请选择要修改的导航！');
            return false;
        }
        var name = $("#module_nav input[name='name']").val();
        var sort = $("#module_nav input[name='sort_order']").val();
        if (name == null || name == undefined || name == "" || sort == null || sort == undefined || sort == "") {
            alertMsg.info('导航名称 和 排序 必填！');
            return false;
        }

        var data = {
            "module_id": id,
            "name": name,
            "sort_order": sort
        };
        $.ajax({
            url: "Module/SaveNav",
            data: data,
            dataType: 'json',
            success: function (data) {
                navTab.reload(data.forwardUrl);
                //延迟500毫秒执行消息提示
                setTimeout(function () {
                    alertMsg.correct(data.message);
                }, 500);

            }
        });
    },

    //删除菜单
    NavDelete: function (id, name) {
        if (id != null && id != undefined && id != "") {
            alertMsg.confirm('确定要删除 ' + name + ' 吗？注意：导航以下的菜单会同时删除！', { okCall: function () {
                $.ajax({
                    url: "Module/DeleteMenu",
                    data: { 'ids': id, 'isNav': true },
                    dataType: 'json',
                    success: function (data) {
                        if (data.statusCode == DWZ.statusCode.ok) {
                            alertMsg.correct(data.message);
                            navTab.openTab(data.navTabId, data.forwardUrl, { title: data.title, fresh: true });
                        }
                    }
                });
            }
            });
        }
        else {
            alertMsg.info('请选择要删除的菜单！')
            return false;
        }
    },
    //批量删除
    batchDelete: function (obj) {
        var jq = $(obj);
        // debugger;
        var attr = new Array();

        jq.closest(".module_content").find(".checked > input").each(function () {
            attr.push($(this).val());
        });
        if (attr != null && attr != undefined && attr.length > 0) {
            var ids = attr.join(",");
            alertMsg.confirm('确定要删除勾选的菜单吗？', { okCall: function () {
                $.ajax({
                    url: "Module/DeleteMenu",
                    data: { 'ids': ids },
                    dataType: 'json',
                    success: function (data) {
                        if (data.statusCode == DWZ.statusCode.ok) {
                            alertMsg.correct(data.message);
                            navTab.openTab(data.navTabId, data.forwardUrl, { title: data.title, fresh: true });
                        }
                    }
                });
            }
            });
        }
        else {
            alertMsg.info('请选择要删除的菜单！')
            return false;
        }
    },

    //点击加载菜单
    ClickMenu: function (obj, id) {
        var tabsdiv = $(obj);
        $.ajax({
            url: "Module/GetMenu",
            data: { 'id': id },
            dataType: 'json',
            success: function (data) {
                if (data.statusCode == DWZ.statusCode.ok && data.Data != undefined) {

                    var temp = tabsdiv.closest(".module_content");
                    //先清空权限
                    temp.find("input[name='authority']").attr("checked", false);
                    temp.find("input[name='name']").val(data.Data.name);
                    temp.find("input[name='icon_url']").val(data.Data.icon_url);
                    temp.find("input[name='front_url']").val(data.Data.front_url);
                    temp.find("input[name='url']").val(data.Data.url);
                    temp.find("select[name='parent_id']").val(data.Data.parent_id);
                    temp.find("input[name='application_id']").val(data.Data.application_id);
                    temp.find("input[name='module_id']").val(data.Data.module_id);
                    temp.find("textarea[name='menu_params']").val(data.Data.menu_params);
                    temp.find("#module_del").attr("menuname", data.Data.name);
                    temp.find("#module_del").attr("moduleid", data.Data.module_id);

                    if (data.Data.is_view === 1) {
                        temp.find("input[name='is_view']").attr("checked", "checked");
                    }
                    else {
                        temp.find("input[name='is_view']").attr("checked", false);
                    }

                    if (data.Data.is_menu === 1) {
                        temp.find("input[name='is_menu']").attr("checked", "checked");
                    }
                    else {
                        temp.find("input[name='is_menu']").attr("checked", false);
                    }

                    if (data.Data.enabled === 1) {
                        temp.find("input[name='enabled']").attr("checked", "checked");
                    }
                    else {
                        temp.find("input[name='enabled']").attr("checked", false);
                    }

                    if (data.Data.sort_order != null && data.Data.sort_order != undefined) {
                        temp.find("input[name='sort_order']").next("label").hide();
                        temp.find("input[name='sort_order']").val(data.Data.sort_order);
                    }
                    if (data.Description) {
                        var strs = data.Description.split(","); //字符分割 
                        for (i = 0; i < strs.length; i++) {
                            temp.find("input[name='authority'][value='" + strs[i] + "']").attr("checked", "checked");
                        }
                    }

                }
            }
        });
    },

    SaveMenu_Edit: function (obj, callbackfn) {
        var module_id = $(obj).find("input[name='module_id']").val();
        if (module_id != undefined && module_id != null && module_id != "") {
            if (confirm("确定要保存菜单吗？")) {
                $(obj).find("input[name='authoritys']").val($(obj).find("input[name='authority']").getCheckBoxValue().toString());
                return validateCallback(obj, callbackfn);
            }
            else {
                return false;
            }
        }
        else {
            alertMsg.info('请选择要修改的菜单！');
            return false;
        }
    },

    SaveMenu_Dialog: function (obj, callbackfn) {
        var name = $(obj).find("input[name='name']").val();
        if (name == null || name == undefined || name == "") {
            alertMsg.error("请填写菜单名称！");
            return false;
        }
        if (confirm("确定要保存菜单吗？")) {
            $(obj).find("input[name='authoritys']").val($(obj).find("input[name='authority']").getCheckBoxValue().toString());
            return validateCallback(obj, callbackfn);
        }
        else {
            return false;
        }

    },

    //回调
    navTabAjaxDone: function (data) {
        if (data.statusCode == DWZ.statusCode.ok) {

            navTab.openTab(data.navTabId, data.forwardUrl, { title: data.title, fresh: true });
            alertMsg.correct(data.message);

        }
        else if (data.statusCode == DWZ.statusCode.error) {
            alertMsg.error(data.message);
        }
    },

    dialogAjaxDone: function (data) {
        if (data.statusCode == DWZ.statusCode.ok) {
            $.pdialog.closeCurrent();
            navTab.openTab(data.navTabId, data.forwardUrl, { title: data.title, fresh: true });
            setTimeout(function () {
                alertMsg.correct(data.message);
            }, 500);

        }
        else if (data.statusCode == DWZ.statusCode.error) {
            alertMsg.error(data.message);
        }
    }
}


